package com.chtw.oasystem.controller;

import com.chtw.oasystem.bean.Position;
import com.chtw.oasystem.common.response.RespUtils;
import com.chtw.oasystem.service.impl.PositionServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author chenhao03
 * @create 2020-11-21 10:22
 */
@ResponseBody
@RestController
@RequestMapping("/position")
public class PositionController {
    private static final Logger logger = LoggerFactory.getLogger(PositionController.class);

    @Autowired
    private PositionServiceImpl positionService;

    @GetMapping("/list")
    public RespUtils<List<Position>> list(){
        try{
            return RespUtils.success(positionService.list());
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/info")
    public RespUtils<Position> info(int id){
        try{
            return RespUtils.success(positionService.info(id));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @PostMapping("/create")
    public RespUtils<Boolean> create(HttpSession session, @RequestBody Position position){
        try{
            return RespUtils.success(positionService.create(session, position));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @PostMapping("/edit")
    public RespUtils<Boolean> edit(HttpSession session, @RequestBody Position position){
        try{
            return RespUtils.success(positionService.edit(session, position));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    public RespUtils<Boolean> edit(HttpSession session, int id){
        try{
            return RespUtils.success(positionService.delete(session, id));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }
}
